#ifndef _BSP_ECC_H
#define _BSP_ECC_H

#include <stdint.h>

#define ECC_CURVE_NIST_P192    0x0001
#define ECC_CURVE_NIST_P256    0x0002
#define ECC_CURVE_SECP256K1    0x0003

#define u64 uint64_t
#define u32 uint32_t
#define u16 uint16_t
#define u8 uint8_t

#define EINVAL (22)
#define ENOMEM (12)
#define EAGAIN (11)
/**
 * ecc_is_key_valid() - Validate a given ECDH private key
 *
 * @curve_id:           id representing the curve to use
 * @ndigits:            curve's number of digits
 * @private_key:        private key to be used for the given curve
 * @private_key_len:    private key length
 *
 * Returns 0 if the key is acceptable, a negative value otherwise
 */
int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
             const u64 *private_key, unsigned int private_key_len);

/**
 * ecc_gen_privkey() -  Generates an ECC private key.
 * The private key is a random integer in the range 0 < random < n, where n is a
 * prime that is the order of the cyclic subgroup generated by the distinguished
 * point G.
 * @curve_id:       id representing the curve to use
 * @ndigits:        curve number of digits
 * @private_key:    buffer for storing the generated private key
 *
 * Returns 0 if the private key was generated successfully, a negative value
 * if an error occurred.
 */
int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey);

/**
 * ecc_make_pub_key() - Compute an ECC public key
 *
 * @curve_id:       id representing the curve to use
 * @ndigits:        curve's number of digits
 * @private_key:    pregenerated private key for the given curve
 * @public_key:     buffer for storing the generated public key
 *
 * Returns 0 if the public key was generated successfully, a negative value
 * if an error occurred.
 */
int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
             const u64 *private_key, u64 *public_key);

/**
 * ecdsa_verify() - verify by public_key, hash, and signature, all param is bigendian uint64_t array,
 *
 * @curve_id:        id representing the curve to use
 * @p_signature:     ecdsa signature
 * @p_hash:          hash
 * @public_key:      buffer for storing the generated public key
 *
 * Returns 0 if the verfiy successfully, a negative value
 * if an error occurred.
 */
int ecdsa_verify(const unsigned int curve_id, const u64 *public_key, const u64 *p_hash, const u64 *p_signature);

/**
 * ecdsa_sign() - sign by private_key, hash, and signature, all param is bigendian uint64_t array,
 *
 * @curve_id:       id representing the curve to use
 * @p_signature:    ecdsa signature buffer to store(write with bigendian array, local cpu endian uint64_t value)
 * @p_hash:         hash
 * @private_key:    buffer for storing the generated private key
 *
 * Returns 0 if the verfiy successfully, a negative value
 * if an error occurred.
 */
int ecdsa_sign(const unsigned int curve_id, const u64 *private_key, const u64 *p_hash, u64 *p_signature);

/**
 * register_external_get_random_number() - register external random number function,
 *
 * @external:   external function
 *
 * Returns 0 if the verfiy successfully, a negative value
 * if an error occurred.
 */
int register_external_get_random_number(int (*external)(u64 *p_vli, unsigned int nbytes));

/**
 * ecc_big_u8_to_little_u64() - swap bigendian bytes to bigendian uint64_t array,
 * little endian uint64_t value
 *
 * @in     bigendian bytes
 * @out    bigendian uint64_t array,little endian uint64_t value
 * @len    len of in
 *
 */
void ecc_big_u8_to_little_u64(const u8 *in, u64 *out, unsigned int len);

#endif

